import 'dart:developer';

import 'package:flutter/foundation.dart';

/// 日志输出
class LogUtils {
  static LogLevel _logLevel = LogLevel.all;
  static String _tag = "LogUtils";

  static void init({LogLevel level = LogLevel.all, String? tag}) {
    _logLevel = level;
    _tag = tag ?? _tag;
  }

  static void e(Object object, {String? tag, bool showLine = true}) {
    if (kReleaseMode) {
      return;
    }
    if (_logLevel == LogLevel.error || _logLevel == LogLevel.all) {
      _printLog('e:', object, showLine, tag: tag);
    }
  }

  static void v(Object object, {String? tag, bool showLine = false}) {
    if (kReleaseMode) {
      return;
    }
    if (_logLevel == LogLevel.verbose || _logLevel == LogLevel.all) {
      _printLog('v:', object, showLine, tag: tag);
    }
  }

  static void _printLog(String stag, Object object, bool showLine, {String? tag}) {
    StringBuffer sb = StringBuffer();
    if (showLine) {
      log("------------------------------------------------------------");
    }
    sb.write(DateTime.now());
    sb.write((tag == null || tag.isEmpty) ? " $_tag-$stag" : " $tag-$stag");
    sb.write(object);
    var msg = sb.toString();
    // while (msg.length > 600) {
    //   var sub = msg.substring(0, 800);
    //   log(sub);
    //   msg = msg.substring(800);
    // }
    log(msg);
    if (showLine) {
      log("------------------------------------------------------------");
    }
  }
}

enum LogLevel {
  /// 不输出日志
  none,

  /// 输出verbose级日志
  verbose,

  /// 输出error级日志
  error,

  /// 输出全部日志
  all,
}
